package com.xd.clap.utils;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeData;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;
import com.xd.clap.commons.Const;
import com.xd.clap.config.WxMaConfiguration;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;


@Slf4j
public class SendMsg {
    private static final String sendMsgUrl = "http://www.dh3t.com/json/sms/SubmitWithSign";
    private static final String account = "dh47276";
    private static final String password = "332e4f2fd8e6cf1df014595d456e613a";
    private static final String adminPhoneList = "13979979644,13979901215";
    private static final String msg = "【湘东区创建办】您单位负责的点位有待处理问题，请登录湘东文明创建小程序查看并及时上传处理结果！";
    private static final String urgeMsg = "【湘东区创建办】提醒！您单位负责的点位有待处理问题较长时间未处理，请登录湘东文明创建小程序查看并及时上传处理结果！";
    public static Boolean sendMsg(String phoneList){
        return getBoolean(phoneList, msg);
    }

    public static Boolean sendUrgeMsg(String phoneList){
        return getBoolean(phoneList, urgeMsg);
    }

    private static Boolean getBoolean(String phoneList, String urgeMsg) {
        JSONObject data = new JSONObject();
        data.put("account", account);
        data.put("password", password);
        data.put("phones", phoneList);
        data.put("content", urgeMsg);
        String response = HttpRequest.post(sendMsgUrl).acceptJson().send(data.toString()).body();
        JSONObject jsonObject = JSONObject.parseObject(response);
        System.out.println("jsonObject:"+jsonObject);
        return jsonObject.getString("result").equals("0");
    }

    /**
     * 微信订阅消息, 给用户发送
     * @param openId 用户openId
     * @return java.lang.Boolean true: 发送成功, false: 发送失败
     * @author 熊林飞
     * @date: 2023/3/11 15:05
     */
    public static Boolean pushMessage(String openId) {

        WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();

        //跳转小程序页面路径 pages/index-goods/index-goods pages/index/index
        subscribeMessage.setPage("pages/index/index");
        //模板消息id
        subscribeMessage.setTemplateId(Const.WX_TEMPLATE_ID);
        //给谁推送 用户的openid （可以调用根据code换openid接口)
        subscribeMessage.setToUser(openId);
        //==========================================创建一个参数集合========================================================
        ArrayList<WxMaSubscribeData> wxMaSubscribeData = new ArrayList<>();
        //消息内容：必须和微信后台配置一致
        wxMaSubscribeData.add(new WxMaSubscribeData("thing13", "请及时处理"));
        wxMaSubscribeData.add(new WxMaSubscribeData("time11", DateTime.now().toString()));
        //把集合给大的data
        subscribeMessage.setData(wxMaSubscribeData);
        //=========================================封装参数集合完毕========================================================

        try {
            //获取微信小程序配置：
            final WxMaService wxService = WxMaConfiguration.getMaService(Const.APPID);
            //进行推送
            wxService.getMsgService().sendSubscribeMsg(subscribeMessage);
            return true;
        } catch (Exception e) {
            log.error("微信推送订阅消息异常======{}=====",e.getMessage());
        }
        return false;
    }
}
